package eu.livotov.tdv.core;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action;
import com.vaadin.event.LayoutEvents;
import com.vaadin.event.ShortcutAction;
import com.vaadin.terminal.StreamResource;
import com.vaadin.ui.*;
import eu.livotov.tdv.DocumentViewer;
import eu.livotov.tdv.api.DocumentRasterProvider;
import eu.livotov.tdv.api.PageRotation;
import eu.livotov.tdv.api.RasterizedPageProperties;
import eu.livotov.tpt.TPTApplication;

import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: sionin
 * Date: 11.05.11
 * Time: 12:21
 * To change this template use File | Settings | File Templates.
 */
public class ThumbnailPanel extends VerticalLayout implements Table.ColumnGenerator {

    private DocumentViewer viewer = null;
    private Table pagesTable = new Table();
    private int thWidth = 0;
    private int thHeight = 0;
    private DocumentRasterProvider source = null;

    private float currentZoom = 1.0f;

    public ThumbnailPanel(DocumentViewer viewer, int width, int height) {
        super();
        this.viewer = viewer;
        this.source = viewer.getDocument();
        this.thWidth = width - 30;
        this.thHeight = height;
        setupTableRenderer();
    }

    private void setupTableRenderer() {
        removeAllComponents();


        try {
            pagesTable = new Table();
            pagesTable.setPageLength(5);
            pagesTable.setCacheRate(0.1);

            pagesTable.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
            pagesTable.setImmediate(true);
            pagesTable.setSelectable(true);

            BeanItemContainer<SinglePageModePageBean> ds = new BeanItemContainer<SinglePageModePageBean>(SinglePageModePageBean.class);

            for (int pn = 1; pn <= source.getPagesCount(); pn++) {
                ds.addBean(new SinglePageModePageBean(pn));
            }

            pagesTable.setContainerDataSource(ds);
            pagesTable.setVisibleColumns(new String[]{"page"});
            pagesTable.addGeneratedColumn("page", this);
            pagesTable.setColumnWidth("page", thWidth);
            pagesTable.setSizeFull();

            addComponent(pagesTable);
            setExpandRatio(pagesTable, 1.0f);

        } catch (Throwable e) {
            addComponent(new Label("Error: " + e.getMessage()));
        }
    }

    public Component generateCell(Table table, Object itemId, Object columnId) {
        final SinglePageModePageBean pageBean = (SinglePageModePageBean) itemId;
        String pageNameId = TdvUtils.md5("" + source.getDocumentId() + pageBean.getPageNumber() + currentZoom) + ".jpg";

        StreamResource res = new StreamResource(new StreamResource.StreamSource() {

            public InputStream getStream() {
                return source.getPageThumbnail(pageBean.getPageNumber(), thWidth, thHeight);
            }
        }, pageNameId, TPTApplication.getCurrentApplication());

        final VerticalLayout vl = new VerticalLayout();

        RasterizedPageProperties props = source.getPageThumbnailProperties(pageBean.getPageNumber(), thWidth);

        Embedded img = new Embedded("", res);
        img.setType(Embedded.TYPE_IMAGE);
        img.setWidth(props.getWidth() + "px");
        img.setHeight(props.getHeight() + "px");

        Label label = new Label(pageBean.getPageNumber() + "");
        label.setWidth(null);
        vl.addComponent(img);
        vl.addComponent(label);
        vl.setData(pageBean.getPageNumber());
        vl.setComponentAlignment(img, Alignment.TOP_CENTER);
        vl.setComponentAlignment(label, Alignment.TOP_CENTER);
        vl.addListener(new LayoutEvents.LayoutClickListener(){

            public void layoutClick(LayoutEvents.LayoutClickEvent layoutClickEvent) {
                viewer.goPage((Integer)vl.getData());
            }
        });

        return vl;
    }
}
